package com.heima.auth.listen;

import com.alibaba.fastjson.JSON;
import com.heima.auth.common.result.ResponseResult;
import com.heima.auth.pojo.NewsScanResult;
import com.heima.auth.pojo.ScanResult;
import com.heima.auth.pojo.WmNews;
import com.heima.auth.service.AuthService;
import com.heima.common.constants.MessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

//@Component
@Slf4j
public class AutoScanListener {
    @Autowired
    AuthService authService;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @RabbitListener(queuesToDeclare = @Queue(MessageConstants.WM_NEWS_AUTO_SCAN_QUEUE))
    public void autoScan(String wmNewsStr){
        log.info("接收到待审核消息 ==> {}",wmNewsStr);
        // 1. 对文章进行审核 得到审核结果
        WmNews wmNews = JSON.parseObject(wmNewsStr, WmNews.class);
        ResponseResult<ScanResult> result = authService.wmNewsAuth(wmNews);
        // 2. 封装审核结果消息对象
        NewsScanResult newsScanResult = new NewsScanResult(result.getData());
        newsScanResult.setNewsId(wmNews.getId());
        // 3. 发送审核结果回调通知
        rabbitTemplate.convertAndSend(MessageConstants.WM_NEWS_AUTO_SCAN_RESULT_QUEUE,newsScanResult);
    }
}
